{extend name="layout/base"}

{block name="content"}

<table id="bill" lay-filter="test"></table>
<div id="view" style="display: none;">
</div>
<script type="text/html" id="headerBal">
    <a lay-event="add" class="layui-btn layui-btn-primary">添加</a>
</script>
<script type="text/html" id="status" >
    {{# if(d.status == 2){ }}
    <span class="layui-badge layui-bg-blue">分账成功</span>
    {{# }else if(d.status==-2){ }}
    <span class="layui-badge layui-bg-cyan">已关单</span>
    {{# }else{}}
    <span class="layui-badge">未分账</span>
    {{# }}}
</script>
<script type="text/html" id="done" >
    {{# if(d.status == -1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">去分账</a>
    {{# }}}
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="detail">详情</a>
</script>
<script type="text/html" id="demo">
    <div class="layui-card">
        <div class="layui-card-header">
            该订单总金额为<span style="color:#FF5722;font-size:16px">{{d.total_fee/100}}</span>元，
            订单支付时间：{{layui.util.toDateString(d.pay_time*1000, 'yyyy-MM-dd HH:mm:ss')}}，
            会员卡号：{{d.card_no}}
        </div>
        <div class="layui-card-header">
            分账给：{{d.mername}}，
            分账金额:<span  style="color:#FF5722;font-size:16px">{{d.sharing_amount/100}}</span>元
        </div>
        <div class="layui-card-body">
            <div>商家信息</div>
            <div style="padding:0 30px">
                <div>商家真实姓名：{{d.name}}</div>
                <div>手机号：{{d.mer_mobile}}</div>
                <div>注册时间：{{layui.util.toDateString(d.mer_retime*1000, 'yyyy-MM-dd HH:mm:ss')}}</div>
            </div>
            <div>
                <div>关单备注：</div>
                <div><textarea name="note" required lay-verify="required" placeholder="请输入关单原因：" class="layui-textarea" id="note"></textarea></div>
            </div>
            <div style="margin-top: 20px;text-align: center;">
                <a class="layui-btn layui-btn-sm layui-btn-danger" data-id="{{d.shorid}}" onclick="closeorder(this)">关单</a>
                <a class="layui-btn layui-btn-sm layui-btn-normal" data-id="{{d.shorid}}" onclick="sure(this)">确认</a>
                <a class="layui-btn layui-btn-sm layui-btn-primary" onclick="cancel()">取消</a>
            </div>
        </div>
    </div>
</script>
<script>
layui.use(['jquery', 'table','form','laydate','laytpl'], function () {
    var $ = layui.jquery,
        table = layui.table,
        laytpl = layui.laytpl;
    
    var accountlist = table.render({
        elem: '#bill',
        url: 'listsSharingOrder',
        page: true, 
        cols: [[
        {field: 'shorid', title: 'id'},
        {field: 'shor_no', title: '分账单号'},
        {field: 'amount', title: '分账金额(元)',templet:function(d){
            return d.amount/100
        }},    
        {field: 'status', title: '分账结果',templet:'#status'},
        {field:'note',title:'关单原因'},
        {field:'time',title:'时间',templet:"<div>{{layui.util.toDateString(d.time*1000,'yyyy-MM-dd HH:mm:ss')}}</div>"},
        {field:'done',title:'操作',templet:"#done"}
        ]]
    });

    var getTpl = document.getElementById('demo').innerHTML,
        view = document.getElementById('view');
    var index
    //监听工具条 
    table.on('tool(test)', function(obj){
        var data = obj.data; 
        var layEvent = obj.event;
        if(layEvent === 'edit'){ //查看
            $.ajax({
                type:'post',
                data:{
                    shorid:data.shorid,
                    merid:data.merid
                },
                url:'getSharingOrder',
                success:function(res){
                    if(res.code == 0){
                        res.msg.shorid = data.shorid
                        laytpl(getTpl).render(res.msg, function(html){
                            view.innerHTML = html;
                        });
                        index = layer.open({
                            type: 1,
                            area:'70%', 
                            content:$('#view')
                        }) 
                    }
                }
            })
        }else if(layEvent == 'detail'){
            window.location.href = 'show?shorid='+data.shorid
        } 
    }); 
    window.closeorder = function(obj){
        var id = $(obj).data('id')
        var note = $("#note").val()
        if(note == ''){
            layer.msg('请输入关单原因');
            return false
        }
        $.ajax({
            type:'post',
            data:{
                shorid:id,
                note:note
            },
            url:'closeSharingOrder',
            success:function(res){
                if(res.code == 0){
                    layer.close(index)
                    accountlist.reload()
                }
            }
        })
    };
    window.sure = function(obj){
        var id = $(obj).data('id')
        $.ajax({
            type:'post',
            data:{
                shorid:id
            },
            url:'profitsharing',
            success:function(res){
                if(res.code == 0){
                    layer.close(index)
                    accountlist.reload()
                }
            }
        })
    };
    window.cancel = function(){
        layer.close(index)
    }  
}) 
</script>
{/block}